<?php

/**
 * ECSHOP 管理中心优惠活动管理
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: favourable.php 17217 2011-01-19 06:29:08Z liubo $
 */
 
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');

/*------------------------------------------------------ */
//-- 活动列表页
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'list')
{
    admin_priv('users_manage');


    /* 模板赋值 */
    $smarty->assign('full_page',   1);
    $smarty->assign('ur_here',     '店铺列表');
    $smarty->assign('action_link', array('href' => 'mshop.php?act=add', 'text' => '添加店铺'));

    $list = mshop_list();
// print_r($list);exit;

    $smarty->assign('mshop_list', $list['item']);
    $smarty->assign('filter',          $list['filter']);
    $smarty->assign('record_count',    $list['record_count']);
    $smarty->assign('page_count',      $list['page_count']);

    $sort_flag  = sort_flag($list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    if($_REQUEST['chose_pid'])
        $smarty->assign('chose_pid', 1);


    /* 显示商品列表页面 */
    assign_query_info();
    $smarty->display('mshop_list.htm');
}
/*------------------------------------------------------ */
//-- 分页、排序、查询
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'unlock_profit')
{
    $mshop_id=$_GET['id']?$_GET['id']:"";
    $result=0;

    if($mshop_id)
    {
      $sql=" update  ".$GLOBALS['ecs']->table('mshop')." set sum_profit=sum_profit+lock_profit,lock_profit=0 where mshop_id='$mshop_id'";
      $result=$db->query($sql);
    }
    if($result)
    {
       $sys_mes="解除佣金锁定，加入可提现佣金成功。";
    }
    else
    {
         $sys_mes="解除佣金锁定，加入可提现佣金失败。";
    }
            /* 清除缓存 */
    clear_cache_files();

    $links[] = array('text' => '返回店铺列表', 'href' => 'mshop.php?act=list&' . list_link_postfix());
    sys_msg($sys_mes);
    exit;
    
} 

/*------------------------------------------------------ */
//-- 分页、排序、查询
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'query')
{
    $list = mshop_list();

    $smarty->assign('mshop_list', $list['item']);
    $smarty->assign('filter',          $list['filter']);
    $smarty->assign('record_count',    $list['record_count']);
    $smarty->assign('page_count',      $list['page_count']);

    $sort_flag  = sort_flag($list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    if(!empty($_REQUEST['chose_pid']))
    {$smarty->assign('chose_pid',1);}

    make_json_result($smarty->fetch('mshop_list.htm'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));
}

/*------------------------------------------------------ */
//-- 删除
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{
    check_authz_json('users_manage');

    $id = intval($_GET['id']);
    $mshop = mshop_info($id);

    if (empty($mshop))
    {
        make_json_error($_LANG['favourable_not_exist']);
    }
    $name = $mshop['user_name'];


    $sql=" delete from ".$GLOBALS['ecs']->table('mshop')." where mshop_id=".$id;
  
    $status=$GLOBALS['db']->query($sql);
    if($status)
    {
         $m_user['mshop_id']='';
         $db->autoExecute($ecs->table('users'), $m_user, 'UPDATE', "mshop_id = '$id'"); 
    }

    /* 记日志 */
    admin_log('删除'.$mshop['user_name'].'的店铺'.$id, 'remove', 'mshop');

    /* 清除缓存 */
    clear_cache_files();

    $url = 'mshop.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
    ecs_header("Location: $url\n");
    exit;
}

/*------------------------------------------------------ */
//-- 批量操作
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'batch')
{

    /* 取得要操作的记录编号 */
    if (empty($_POST['checkboxes']))
    {
        sys_msg($_LANG['no_record_selected']);
    }
    else
    {
        /* 检查权限 */
        admin_priv('users_manage');


        $ids = $_POST['checkboxes'];
        if (isset($_POST['drop']))
        {
           

            /* 删除记录 */
            $sql = "DELETE FROM " . $ecs->table('mshop') .
                    " WHERE mshop_id " . db_create_in($ids);
            $db->query($sql);

            $sql = "update  " . $ecs->table('users') .
                    " set  mshop_id='' ".
                    " WHERE mshop_id " . db_create_in($ids);
            $db->query($sql);


            /* 记日志 */
            admin_log('', 'batch_remove', 'mshop');

            /* 清除缓存 */
            clear_cache_files();

            $links[] = array('text' => '返回店铺列表', 'href' => 'mshop.php?act=list&' . list_link_postfix());
            sys_msg($_LANG['batch_drop_ok']);
        }
        elseif (isset($_POST['cash_money']))
        {


            /*提现记录 */
            require_once('Wxpay_delivery.php'); 
            $Wxpay_delivery=new Wxpay_delivery();
            $sql="SELECT m.mshop_id,m.sum_profit,u.openid,u.user_id ".
                "FROM " . $GLOBALS['ecs']->table('mshop')." as m  ".
                " left join ". $GLOBALS['ecs']->table('users') ." as u on m.mshop_id=u.mshop_id "
                ." where m.sum_profit>=1 and m.mshop_id ".db_create_in($ids);
             $data_list=$GLOBALS['db']->getAll($sql);
             $error_num=0;
             $right_num=0;

            foreach ($data_list as $key => $value) {
                $mshop_id=$value['mshop_id'];
                $user_id=$value['user_id'];
                $openid=$value['openid'];
                $cash_money=intval($value['sum_profit']);
                $status=$Wxpay_delivery->busSendProfit($mshop_id,$user_id,$openid,$cash_money);
                if($status)
               $right_num++;
               else
                $error_num++;
            }

            $sys_mes="成功提现". $right_num."人，提现失败".$error_num."人，查看佣金提现记录了解详情。";





            /* 记日志 */
             admin_log('成功全部提现'.$mshop['mshop_id'], 'add', 'mshop');

            /* 清除缓存 */
            clear_cache_files();

            $links[] = array('text' => '返回店铺列表', 'href' => 'mshop.php?act=list&' . list_link_postfix());
            sys_msg($sys_mes);
        }
    }
}


/*------------------------------------------------------ */
//-- 添加、编辑
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'add' || $_REQUEST['act'] == 'edit')
{
    /* 检查权限 */
    admin_priv('users_manage');

    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'add';
    $smarty->assign('form_action', $is_add ? 'insert' : 'update');


    /* 初始化、取得优惠活动信息 */
    if ($is_add)
    {
        $sql = 'SELECT c.cat_id,c.cat_name ' .
                ' FROM ' . $GLOBALS['ecs']->table('category') . ' as c ' .
                'left join ' . $GLOBALS['ecs']->table('touch_category') . ' as t on t.cat_id = c.cat_id ' .
                "WHERE c.parent_id = 0 AND c.is_show = 1 ORDER BY c.sort_order ASC, c.cat_id ASC";
        $cats =$GLOBALS['db']->getAll($sql);

        $mshop = array(
            'mshop_id'        => 0,
            'cat_ids'      => 0,
            'cash_id'      => 0,
            'sum_profit'    => 0,
            'cats'=>$cats,
            'mshop_pid'=>0,
            'p_user_name'=>0
        );
    }
    else
    {
        if (empty($_GET['id']))
        {
            sys_msg('invalid param');
        }
        $id = intval($_GET['id']);
        $mshop = mshop_info($id);
        if (empty($mshop))
        {
            sys_msg($_LANG['favourable_not_exist']);
        }

    }
    $smarty->assign('mshop', $mshop);


    // print_r($mshop);exit();


    /* 显示模板 */
    if ($is_add)
    {
        $smarty->assign('ur_here', $_LANG['add_favourable']);
        $smarty->assign('is_chose_user', 1);
    }
    else
    {
        $smarty->assign('ur_here', $_LANG['edit_favourable']);
    }
    $href = 'mshop.php?act=list';
    if (!$is_add)
    {
        $href .= '&' . list_link_postfix();
    }
    $smarty->assign('action_link', array('href' => $href, 'text' => $_LANG['favourable_list']));
    assign_query_info();
    $smarty->display('mshop_info.htm');
}

/*------------------------------------------------------ */
//-- 添加、编辑后提交
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'insert' || $_REQUEST['act'] == 'update')
{
    /* 检查权限 */
    admin_priv('users_manage');

    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'insert';

    /* 检查店铺是否重复 */
    $mshop_user_id = intval($_POST['mshop_user_id']) >= 0 ? intval($_POST['mshop_user_id']) : 0;
    $user_id = $_POST['user_id'];
    $sql=" select mshop_id from".$GLOBALS['ecs']->table('users')." where user_id='$mshop_user_id'";
    $mshop_id=$GLOBALS['db']->getOne($sql);
    if ($mshop_id&&$is_add)
    {
        sys_msg($_LANG['act_name_exists']);
    }

    /* 是否设置商品主营类别 */
    if (!isset($_POST['cats']))
    {
        sys_msg($_LANG['pls_set_cats']);
    }
    else
    {
        $cat_ids=implode(',', $_POST['cats']);
    }

    /* 检查可提现佣金 */
    $sum_profit = $_POST['sum_profit']? $_POST['sum_profit'] : 0;
    $mshop_pid = $_POST['mshop_pid']?$_POST['mshop_pid']:0;

    

    /* 提交值 */
    $mshop = array(
        'mshop_id'        => intval($_POST['id']),
        'cat_ids'      => $cat_ids,
        'sum_profit'      => $sum_profit,
        'mshop_pid'=>$mshop_pid

    );

    /* 保存数据 */
    if ($is_add)
    {
        $now_time=time();
        $mshop['ctime']=$now_time;

        $db->autoExecute($ecs->table('mshop'), $mshop, 'INSERT');
        $mshop['mshop_id'] = $db->insert_id();
         
         $m_user['mshop_id']=$mshop['mshop_id'];
         $db->autoExecute($ecs->table('users'), $m_user, 'UPDATE', "user_id = '$mshop_user_id'");
    }
    else
    {
        $db->autoExecute($ecs->table('mshop'), $mshop, 'UPDATE', "mshop_id = '$mshop[mshop_id]'");
         
    }

    /* 记日志 */
    if ($is_add)
    {
        admin_log('添加店铺'.$mshop['mshop_id'], 'add', 'mshop');
    }
    else
    {
        admin_log('编辑店铺'.$mshop['mshop_id'], 'edit', 'mshop');
    }

    /* 清除缓存 */
    clear_cache_files();

    /* 提示信息 */
    if ($is_add)
    {
        $links = array(
            array('href' => 'mshop.php?act=add', 'text' => $_LANG['continue_add_favourable']),
            array('href' => 'mshop.php?act=list', 'text' => $_LANG['back_favourable_list'])
        );
        sys_msg($_LANG['add_favourable_ok'], 0, $links);
    }
    else
    {
        $links = array(
            array('href' => 'mshop.php?act=list&' . list_link_postfix(), 'text' => $_LANG['back_favourable_list'])
        );
        sys_msg($_LANG['edit_favourable_ok'], 0, $links);
    }
}

/*------------------------------------------------------ */
//-- 搜索商品
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'search')
{
    /* 检查权限 */
    check_authz_json('mshop');

    include_once(ROOT_PATH . 'includes/cls_json.php');

    $json   = new JSON;
    $filter = $json->decode($_GET['JSON']);
    $filter->keyword = json_str_iconv($filter->keyword);
    if ($filter->act_range == FAR_ALL)
    {
        $arr[0] = array(
            'id'   => 0,
            'name' => $_LANG['js_languages']['all_need_not_search']
        );
    }
    else
    {
       $arr=$mshop_list;
    }

    if (empty($arr))
    {
        $arr = array(0 => array(
            'id'   => 0,
            'name' => $_LANG['search_result_empty']
        ));
    }

    make_json_result($arr);
}
elseif ($_REQUEST['act'] == 'cash_all_money')
{
           /*提现记录 */
            require_once('Wxpay_delivery.php'); 
            $Wxpay_delivery=new Wxpay_delivery();
            $sql="SELECT m.mshop_id,m.sum_profit,u.openid,u.user_id ".
                "FROM " . $GLOBALS['ecs']->table('mshop')." as m  ".
                " left join ". $GLOBALS['ecs']->table('users') ." as u on m.mshop_id=u.mshop_id "
                ." where m.sum_profit>=1  ";
             $data_list=$GLOBALS['db']->getAll($sql);
             $error_num=0;
             $right_num=0;

            foreach ($data_list as $key => $value) {
                $mshop_id=$value['mshop_id'];
                $user_id=$value['user_id'];
                $openid=$value['openid'];
                $cash_money=intval($value['sum_profit']);
                $status=$Wxpay_delivery->busSendProfit($mshop_id,$user_id,$openid,$cash_money);
                if($status)
               $right_num++;
               else
                $error_num++;
            }

            $sys_mes="成功提现". $right_num."人，提现失败".$error_num."人，查看佣金提现记录了解详情。";
  
   



            /* 记日志 */
             admin_log('成功提现'.$mshop['mshop_id'], 'add', 'mshop');

            /* 清除缓存 */
            clear_cache_files();

            $links[] = array('text' => '返回店铺列表', 'href' => 'mshop.php?act=list&' . list_link_postfix());
            sys_msg($sys_mes);
}

/*
 * 取得店铺列表
 * @return   array
 */
function mshop_list()
{    
    $result = get_filter();
    if ($result === false)
    {
        /* 过滤条件 */
        $filter['mshop_id']    = empty($_REQUEST['mshop_id']) ? '' : trim($_REQUEST['mshop_id']); 
        $filter['keyword']    = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']); 
        $filter['subrank']    = empty($_REQUEST['subrank']) ? '' : trim($_REQUEST['subrank']); 
        $filter['mshop_pid']    = empty($_REQUEST['mshop_pid']) ? '' : trim($_REQUEST['mshop_pid']); 
        $filter['mshop_phone']    = empty($_REQUEST['mshop_phone']) ? '' : trim($_REQUEST['mshop_phone']); 
       //销售额，佣金，冻结佣金
        $filter['min_amount']    = empty($_REQUEST['min_amount']) ? '' : trim($_REQUEST['min_amount']); 
        $filter['max_amount']    = empty($_REQUEST['max_amount']) ? '' : trim($_REQUEST['max_amount']); 
        $filter['max_profit']    = empty($_REQUEST['max_profit']) ? '' : trim($_REQUEST['max_profit']);
        $filter['min_profit']    = empty($_REQUEST['min_profit']) ? '' : trim($_REQUEST['min_profit']); 
        $filter['max_lock']    = empty($_REQUEST['max_lock']) ? '' : trim($_REQUEST['max_lock']);
        $filter['min_lock']    = empty($_REQUEST['min_lock']) ? '' : trim($_REQUEST['min_lock']); 
        $filter['min_frozen']    = empty($_REQUEST['min_frozen']) ? '' : trim($_REQUEST['min_frozen']); 
        $filter['max_frozen']    = empty($_REQUEST['max_frozen']) ? '' : trim($_REQUEST['max_frozen']); 
        

        if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1)
        {
            $filter['mshop_id'] = json_str_iconv($filter['mshop_id']);
            $filter['keyword'] = json_str_iconv($filter['keyword']);
            $filter['subrank'] = json_str_iconv($filter['subrank']);
            $filter['mshop_pid'] = json_str_iconv($filter['mshop_pid']);
            $filter['mshop_phone'] = json_str_iconv($filter['mshop_phone']);

            $filter['min_amount'] = json_str_iconv($filter['min_amount']);
            $filter['max_amount'] = json_str_iconv($filter['max_amount']);
            $filter['max_profit'] = json_str_iconv($filter['max_profit']);
            $filter['min_profit'] = json_str_iconv($filter['min_profit']);
            $filter['max_lock'] = json_str_iconv($filter['max_lock']);
            $filter['min_lock'] = json_str_iconv($filter['min_lock']);
            $filter['min_frozen'] = json_str_iconv($filter['min_frozen']);
            $filter['max_frozen'] = json_str_iconv($filter['max_frozen']);
        }

        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'm.mshop_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

        $where = "";
        if (!empty($filter['mshop_id']))
        {
            $where .= " AND m.mshop_id=" . mysql_like_quote($filter['mshop_id']);
        }
        if (!empty($filter['keyword']))
        {
            $where .= " AND u.user_name LIKE '%" . mysql_like_quote($filter['keyword']) . "%'";
        }

        if (!empty($filter['mshop_pid'])&&!empty($filter['subrank']))
        {
            $mshop_ids=getmshopidbypid($filter['mshop_pid'],$filter['subrank']);
            $w_ids='';
            foreach ($mshop_ids as $key => $value) {
                if($w_ids)
                $w_ids.=","."'$value'";
                else
                 $w_ids="'$value'";
            }
            if($w_ids)
            $where .= " AND m.mshop_id in(" .$w_ids. ")";
        }
        elseif (!empty($filter['mshop_pid']))
        {
            $where .= " AND m.mshop_pid=" . mysql_like_quote($filter['mshop_pid']) ;
        }

        if (!empty($filter['mshop_phone']))
        {
            $where .= " AND u.mobile_phone LIKE '%" . mysql_like_quote($filter['mshop_phone']) . "%'";
        }

        if (!empty($filter['min_amount']))
        {
            $where .= " AND m.self_sum_amount>=" . mysql_like_quote($filter['min_amount']);
        }
        if (!empty($filter['max_amount']))
        {
           $where .= " AND m.self_sum_amount<=" . mysql_like_quote($filter['max_amount']);
        }
        if (!empty($filter['min_profit']))
        {
            $where .= " AND m.sum_profit>=" . mysql_like_quote($filter['min_profit']);
        }
        if (!empty($filter['max_profit']))
        {
           $where .= " AND m.sum_profit<=" . mysql_like_quote($filter['max_profit']);
        }        
        if (!empty($filter['min_lock']))
        {
            $where .= " AND m.lock_profit>=" . mysql_like_quote($filter['min_lock']);
        }
        if (!empty($filter['max_lock']))
        {
           $where .= " AND m.lock_profit<=" . mysql_like_quote($filter['max_lock']);
        }
        if (!empty($filter['min_frozen']))
        {
            $where .= " AND m.frozen_profit>=" . mysql_like_quote($filter['min_frozen']);
        }
        if (!empty($filter['max_frozen']))
        {
           $where .= " AND m.frozen_profit<=" . mysql_like_quote($filter['max_frozen']);
        }

       

      $ts_where=" and u.mshop_id!=0";//没有店铺的会员不用连接
      $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('mshop') ." as m  ".
        " left join ". $GLOBALS['ecs']->table('users') ." as u on u.mshop_id=m.mshop_id ".
        " WHERE 1 $where".$ts_where;
       $filter['record_count'] = $GLOBALS['db']->getOne($sql);

     /* 分页大小 */
        $filter = page_and_size($filter);

        /* 查询 */
        $sql = "SELECT m.*".
                ",u.user_name,u.mobile_phone,u.pay_points  ".
                " FROM " . $GLOBALS['ecs']->table('mshop')." as m  ".
                " left join ". $GLOBALS['ecs']->table('users') ." as u on u.mshop_id=m.mshop_id ".
                " WHERE 1 $where ".$ts_where.
                " ORDER BY $filter[sort_by] $filter[sort_order] ".
                " LIMIT ". $filter['start'] .", $filter[page_size]";
      
        $filter['keyword'] = stripslashes($filter['keyword']);


        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->query($sql);





    $list = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        //主营商品类别
      // if($row['cat_ids'])
      //  {
      //       $cids=explode(',', $row['cat_ids']);
      //       $cid_where='';
      //       foreach ($cids as $k_cid => $v_cid) {
      //           if($cid_where)
      //           $cid_where.=","."'$v_cid'";
      //           else
      //            $cid_where="'$v_cid'";
      //         }

      //       $c_where="is_show=1 and cat_id in(".$cid_where.")";
      //       $sql = ' select cat_id,cat_name from '.$GLOBALS['ecs']->table('category').' where '.$c_where;
            
      //   }
      //   else
      //   {

      //         $sql = 'SELECT c.cat_id,c.cat_name ' .
      //           'FROM ' . $GLOBALS['ecs']->table('category') . ' as c ' .
      //           'left join ' . $GLOBALS['ecs']->table('touch_category') . ' as t on t.cat_id = c.cat_id ' .
      //           "WHERE c.parent_id = 0 AND c.is_show = 1 ORDER BY c.sort_order ASC, c.cat_id ASC";

      //   }  

      //    $cate =$GLOBALS['db']->getAll($sql);

         // $row['cats']=$cate;

          // foreach ($cate as $k_cate => $v_cate) {
          //     if($row['cat_names'])
          //     $row['cat_names'].=','.$v_cate['cat_name'];
          //     else
          //    $row['cat_names']=$v_cate['cat_name'];
          // }
        $row['cat_names']='江左梅郎';
        //推荐人
        if($row['mshop_pid'])
        {
            $sql=" select user_name,user_id from ".$GLOBALS['ecs']->table('users') ." where mshop_id=".$row['mshop_pid'];
            $user_par =$GLOBALS['db']->getRow($sql);
            $row['p_user_id']=$user_par['user_id'];
            $row['p_user_name']=$user_par['user_name'];
        }
        $row['ctime']=date('Y-m-d H:i:s',$row['ctime']);

        $list[] = $row;
    }

    return array('item' => $list, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}

   function mshop_info($id)
   {
        $sql="SELECT m.*,u.user_id,u.user_name,u.mobile_phone,u.pay_points ".
                "FROM " . $GLOBALS['ecs']->table('mshop')." as m  ".
                " left join ". $GLOBALS['ecs']->table('users') ." as u on m.mshop_id=u.mshop_id "
                ." where m.mshop_id=".$id;
        $row=$GLOBALS['db']->getRow($sql);

         $sql = 'SELECT c.cat_id,c.cat_name ' .
                ' FROM ' . $GLOBALS['ecs']->table('category') . ' as c ' .
                'left join ' . $GLOBALS['ecs']->table('touch_category') . ' as t on t.cat_id = c.cat_id ' .
                "WHERE c.parent_id = 0 AND c.is_show = 1 ORDER BY c.sort_order ASC, c.cat_id ASC";
        $cate =$GLOBALS['db']->getAll($sql);
         //主营商品类别
      if($row['cat_ids'])
       {
            $cids=explode(',', $row['cat_ids']);
            foreach ($cate as $key => &$value) {
                if(!(in_array($value['cat_id'],$cids)))
                {
                   $value['no_selected']=1;   
                }
            }

        }
        $row['cats']=$cate;

        // $row['cat_names']='江左梅郎';

        if($row['mshop_pid'])
        {
            $sql=" select user_name,user_id from ".$GLOBALS['ecs']->table('users') ." where mshop_id=".$row['mshop_pid'];
            $user_par =$GLOBALS['db']->getRow($sql);
            $row['p_user_id']=$user_par['user_id'];
            $row['p_user_name']=$user_par['user_name']?$user_par['user_name']:0;
        }

         
        return $row;
   }
   //获取子类的店铺id
   function getmshopidbypid($pid,$subrank)
   {
        $mshop_all=array();
        $sql = " SELECT mshop_id FROM " . $GLOBALS['ecs']->table('mshop') ." WHERE mshop_pid= '$pid'";
        $sec=$GLOBALS['db']->getAll($sql);
        if($subrank==4)
        {
            foreach ($sec as $key => $value) {
               $mshop_all[]=$value['mshop_id'];
            }

             foreach ($sec as $key => $value) {
                 
                  $sql = " SELECT mshop_id FROM " . $GLOBALS['ecs']->table('mshop') ." WHERE mshop_pid= '$value[mshop_id]'";
                  $thr=$GLOBALS['db']->getAll($sql);
                  foreach ($thr as $key2 => $value2) {
                      $mshop_all[]=$value2['mshop_id'];
                  }
            }
        }
        elseif($subrank==3)
        {
             foreach ($sec as $key => $value) {
                 
                  $sql = " SELECT mshop_id FROM " . $GLOBALS['ecs']->table('mshop') ." WHERE mshop_pid= '$value[mshop_id]'";
                  $thr=$GLOBALS['db']->getAll($sql);
                  foreach ($thr as $key2 => $value2) {
                      $mshop_all[]=$value2['mshop_id'];
                  }
            }           
        }

        return  $mshop_all;

   }

   
?>